package com.qf.blogsystem.controller.website;

import com.alibaba.fastjson.JSON;
import com.qf.blogsystem.core.controller.BaseServlet;
import com.qf.blogsystem.core.utils.BaseConstants;
import com.qf.blogsystem.core.utils.EmailUtils;
import com.qf.blogsystem.dto.EmailDto;
import com.qf.blogsystem.entity.User;
import com.qf.blogsystem.service.UserService;
import com.qf.blogsystem.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/UserServlet/*")
public class UserServlet extends BaseServlet {
    /**
     * 创建用户业务层实现对象
     */
    private UserService userService = new UserServiceImpl();

    /**
     * 用户注册
     * @return String
     * @throws Exception
     */
    public String register() throws Exception {
        //接收用户注册信息
        String json = request.getReader().readLine();
        //转换成用户对象
        User user = JSON.parseObject(json, User.class);
        //判断当前用户名或邮箱是否已经被注册
//        User userByUsername = userService.byUsername(user);
//        if (userByUsername!=null){
//            //用户名已经被注册
//            Map map=new HashMap();
//            map.put("usernameMsg","用户名已存在!");
//            outView("view/site/register",map);
//            return null;
//        }
//        User userByEmail = userService.byEmail(user);
//        if (userByEmail!=null){
//            //邮箱已经被注册
//            Map map=new HashMap();
//            map.put("emailMsg","邮箱已被注册!");
//            outView("view/site/register",map);
//            return null;
//        }
        //调用业务层的注册方法
        boolean result = userService.save(user);
        if (result){
            String activationCode = user.getActivationCode();
            //给用户的邮箱发送激活账号的连接
            EmailDto emailDto = new EmailDto();
            emailDto.setTilte("Blog新用户注册账号激活");
            emailDto.setToUser(user.getEmail());
            emailDto.setContent("<a href = 'http://localhost:8080/UserServlet/activation?code=" + activationCode + "'>点击这里激活</a>");
            EmailUtils.sendEmailInfoUser(emailDto);
            //提示用户
            return outJson(200,"注册成功!");
        }else {
            return BaseConstants.BASE_SCRIPT + ":注册失败!";
        }
    }

    /**
     * 激活账号
     * @return String
     * @throws Exception
     */
    public String activation() throws Exception {
        //获取激活码
        String activationCode = request.getParameter("code");
        //验证
        User user = userService.byActivationCode(activationCode);
        if (user == null) {
            return BaseConstants.BASE_SCRIPT + ":激活码有误";
        } else {
            user.setStatus(1);
            userService.update(user);
            return BaseConstants.BASE_REDIRECT + ":PageServlet/view/site/login.html";
        }
    }

    /**
     * 用户登录
     * @return String
     * @throws Exception
     */
    public String login() throws Exception {
        //接收用户注册信息
        String json = request.getReader().readLine();
        //转换成用户对象
        User user = JSON.parseObject(json, User.class);
        //验证账号密码
        user = userService.login(user.getUsername(), user.getPassword());
        //判断认证成功
        if (user == null) {
            return outJson(100, "用户名或密码错误");
        }
        //登录成功
        user.setPassword(null);
        request.getSession().setAttribute("loginUser", user);
        return outJson(200, "登录成功");
    }

    /**
     * 用户注销登录
     * @throws Exception
     */
    public void logout() throws Exception {
        //让sessionId失效
        request.getSession().invalidate();
        //跳转到登录界面
        outView("view/site/login", null);
    }
}
